using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Dogfight2008
{
  public class InGameState : IState
  {
    IGame game;

    public InGameState(IGame game)
    {
      this.game = game;
    }

    #region IState Members

    public DFState KeyDown(Keys key)
    {
      if (key == Keys.Escape)
        return DFState.PressFire;
      switch (key)
      {
        case Keys.A:
          game.PlanePadChange(PlaneName.Red, PlanePad.Lift, PlanePadState.Pressed);
          break;
        case Keys.D:
          game.PlanePadChange(PlaneName.Red, PlanePad.Dive, PlanePadState.Pressed);
          break;
        case Keys.ShiftKey:
          game.PlanePadChange(PlaneName.Red, PlanePad.Fire, PlanePadState.Pressed);
          break;

        case Keys.J:
          game.PlanePadChange(PlaneName.Yellow, PlanePad.Lift, PlanePadState.Pressed);
          break;
        case Keys.L:
          game.PlanePadChange(PlaneName.Yellow, PlanePad.Dive, PlanePadState.Pressed);
          break;
        case Keys.ControlKey:
          game.PlanePadChange(PlaneName.Yellow, PlanePad.Fire, PlanePadState.Pressed);
          break;

        case Keys.R:
          game.ResetGame();
          break;

        default:
          break;
      }
      return DFState.InGame;
    }

    public DFState Tick(double deltaMS)
    {
      game.Tick(deltaMS);
      if (game.GameOver)
        return DFState.GameOver;
      else
        return DFState.InGame;
    }

    public DFState KeyUp(Keys key)
    {
      switch (key)
      {
        case Keys.A:
          game.PlanePadChange(PlaneName.Red, PlanePad.Lift, PlanePadState.Released);
          break;
        case Keys.D:
          game.PlanePadChange(PlaneName.Red, PlanePad.Dive, PlanePadState.Released);
          break;

        case Keys.J:
          game.PlanePadChange(PlaneName.Yellow, PlanePad.Lift, PlanePadState.Released);
          break;
        case Keys.L:
          game.PlanePadChange(PlaneName.Yellow, PlanePad.Dive, PlanePadState.Released);
          break;

        default:
          break;
      }
      return DFState.InGame;
    }

    Uniform U(double v)
    {
      return new Uniform(v);
    }

    public void Render(IRenderer renderer)
    {
      Game g = (Game)game;
      g.Render(renderer);

      //StateUtil.RenderNiceCenterText(renderer, "Simulating game!");

      Uniform y1 = new Uniform(0.05);
      Uniform y2 = new Uniform(0.10);
      Uniform x1 = new Uniform(0.15);
      Uniform x2 = new Uniform(0.85);
      Uniform h1 = new Uniform(0.05);
      Uniform h2 = new Uniform(0.1);

      string left = game.PlayerOnePlanes.ToString();
      string right = game.PlayerTwoPlanes.ToString();

      renderer.Color(Color.Red);
      renderer.TextAt("Planes Left",
        x1, y1, h1, Anchor.Center);
      renderer.TextAt(left, 
        x1, y2, h2, Anchor.Center);

      renderer.Color(Color.Yellow);
      renderer.TextAt("Planes Right",
        x2, y1, h1, Anchor.Center);
      renderer.TextAt(right,
        x2, y2, h2, Anchor.Center);

     // renderer.DrawMesh(Mesh.Backdrop);

    }

    #endregion

    internal void TweakConfig(TweakConfig config)
    {
      Game theGame = game as Game;
      theGame.TweakConfig(config);
    }
  }
}
